﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using CRMS.Models;

namespace CRMS.Views.SelStudy
{
    public partial class frmApplyForRoom : Form
    {
        private BuildingRep buidingRep = new BuildingRep();
        private OpenCourseInfoRep tcRep = new OpenCourseInfoRep();
        public frmApplyForRoom()
        {
            InitializeComponent();
        }

        private void frmApplyForRoom_Load(object sender, EventArgs e)
        {
            cbBuilding.DataSource = buidingRep.GetBuildings();
            cbBuilding.DisplayMember = "TeachingBuildingName";
            cbBuilding.ValueMember = "TeachingBuildingId";
            cbWeekday.Items.AddRange(new object[]{"星期一","星期二","星期三","星期四","星期五","星期六","星期天"});
            cbWeekday.SelectedIndex = 1;
            cbPeriod.Items.AddRange(new object[]{"一、二","一、二、三","四、五","三、四、五","六、七、八","六、七、八、九"});
            cbPeriod.SelectedIndex = 1;
            txtUser.Text = frmMain.userPass;
        }

        private void button_Click(object sender, EventArgs e)
        {
            ToUseClassroom room = new ToUseClassroom();
            room.ClassroomId = ((CRMS.Models.OpenCourseInfoRep.classroom)cbRoom.Items[cbRoom.SelectedIndex]).roomID;
            room.TeachingBuildingId = ((TeachingBuilding)cbBuilding.Items[cbBuilding.SelectedIndex]).TeachingBuildingId;
            room.UserId = txtUser.Text.Trim();
            room.UseDate = dateTimePicker.Value;
            room.Weekday = cbWeekday.Text.Trim();
            room.Period = cbPeriod.Text.Trim();
            room.UseAs = txtUseAs.Text.Trim();
            room.UseStatus = '0';
            if (tcRep.HadApplyThisRoom(room.TeachingBuildingId, room.ClassroomId, room.UserId, room.UseDate, room.Weekday, room.Period))
            {
                MessageBox.Show("你不能在相同时间申请该教室两次！");
            }
            else
            {
                tcRep.Add(room);
                tcRep.Save();
                MessageBox.Show("申请成功，等待管理员批复！");
            }
        }

        private void cbBuilding_SelectedIndexChanged(object sender, EventArgs e)
        {
            cbRoom.Text = String.Empty;
            cbRoom.DataSource = tcRep.getRooms(((TeachingBuilding)cbBuilding.Items[cbBuilding.SelectedIndex]).TeachingBuildingId);
            cbRoom.DisplayMember = "roomID";
            cbRoom.ValueMember = "roomID";
        }
    }
}
